package com.jiulan.hearthearing.ui.course.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseBottomDialog;
import com.jiulan.hearthearing.ui.course.activity.CourseDetailsFMActivity;
import com.jiulan.hearthearing.ui.course.adapter.CoursePlaySpeedSetAdapter;

import java.util.ArrayList;
import java.util.List;

public class CoursePlaySpeedSetDialog extends BaseBottomDialog {
    private Context context;
    private String choose = "";
    private List<String> data = new ArrayList<>();
    private CoursePlaySpeedSetAdapter speedSetAdapter;

    private RecyclerView mRecyclerView;
    private TextView tvCancel;

    public CoursePlaySpeedSetDialog(Context context, float speed) {
        this.context = context;
        if (speed == 0.7f) {
            this.choose = "0.7x";
        } else if (speed == 1.0f) {
            this.choose = "正常倍速";
        } else if (speed == 1.25f) {
            this.choose = "1.25x";
        } else if (speed == 1.5f) {
            this.choose = "1.5x";
        } else if (speed == 2.0f) {
            this.choose = "2.0x";
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_course_fm;
    }

    @Override
    public void bindView(View v) {
        mRecyclerView = v.findViewById(R.id.mRecyclerView);
        tvCancel = v.findViewById(R.id.tvCancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        data.add("0.7x");
        data.add("正常倍速");
        data.add("1.25x");
        data.add("1.5x");
        data.add("2.0x");
        speedSetAdapter = new CoursePlaySpeedSetAdapter(data);
        speedSetAdapter.setClickPosition(choose);
        mRecyclerView.setAdapter(speedSetAdapter);
        speedSetAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                speedSetAdapter.setClickPosition(data.get(position));
                speedSetAdapter.notifyItemChanged(position);
                sendBroadcast(3, data.get(position));
                dismiss();
            }
        });
    }

    private void sendBroadcast(int action, String speed) {
        Intent intent = new Intent(CourseDetailsFMActivity.LOCAL_BROADCAST);
        intent.putExtra("action", action);
        intent.putExtra("speed", speed);
        CourseDetailsFMActivity.localBroadcastManager.sendBroadcast(intent);
    }
}
